package copyengine.utils.tick.node
{
	import copyengine.utils.debug.CELog;

	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;

	public class TweenEffectTickObjectNode extends OldAnimationTickObjectNode
	{
		private var tweenTarget:DisplayObject;
		private var tweenMC:MovieClip;

		private var tweenTargetOriginalPosX:Number;
		private var tweenTargetOriginalPosY:Number;

		private var tweenTargetOriginalMouseEnable:Boolean;
		private var tweenTargetOriginalMouseChildren:Boolean;

		public function TweenEffectTickObjectNode(_target:DisplayObject, _tweenMC:MovieClip, _endCallBackFunction:Function, _repeatTime:int)
		{
			super(_tweenMC, _endCallBackFunction, _repeatTime, OldAnimationTickObjectNode.END_ACTION_REMOVE);
			tweenTarget = _target;

			//tween animation will start in next tick , but current tick the target already add to the stage.
			//so need to set the alpha to 0 in this tick
//			tweenTarget.alpha = 0;

			tweenMC = _tweenMC;
			tweenMC.visible = false;
			tweenMC.gotoAndPlay(1);

			tweenTargetOriginalPosX = tweenTarget.x;
			tweenTargetOriginalPosY = tweenTarget.y;

			if (tweenTarget is DisplayObjectContainer)
			{
				var container:DisplayObjectContainer = tweenTarget as DisplayObjectContainer;
				tweenTargetOriginalMouseEnable = container.mouseEnabled;
				tweenTargetOriginalMouseChildren = container.mouseChildren;
				container.mouseEnabled = container.mouseChildren = false;
			}

			copyTweenMcPropertyToTarget();
		}

		override protected function tickLogic(_elapseTime:int):void
		{
			super.tickLogic(_elapseTime);
			copyTweenMcPropertyToTarget();
		}

		override public function destory():void
		{
			if (tweenTarget is DisplayObjectContainer)
			{
				var container:DisplayObjectContainer = tweenTarget as DisplayObjectContainer;
				container.mouseEnabled = tweenTargetOriginalMouseEnable;
				container.mouseChildren = tweenTargetOriginalMouseChildren;
			}
			super.destory();
		}

		private function copyTweenMcPropertyToTarget():void
		{
			tweenTarget.x = int(tweenTargetOriginalPosX + tweenMC["mc"].x);
			tweenTarget.y = int(tweenTargetOriginalPosY + tweenMC["mc"].y);
			tweenTarget.rotation = tweenMC.mc.rotation;
			tweenTarget.scaleX = tweenMC["mc"].scaleX;
			tweenTarget.scaleY = tweenMC["mc"].scaleY;
			tweenTarget.alpha = tweenMC["mc"].alpha;
			tweenTarget.filters = tweenMC["mc"].filters;
		}
	}
}
